////////////////////////////////////////
// 				Settings			  //
////////////////////////////////////////

// Stata settings:
capture log close
clear all
set more off
version 15


// Working directory:

global path "U:\Research\" //directory where all files must be in

// look of graphs, load packages
set matsize 1000, perm
graph set window fontface "Times New Roman"
ssc install ereplace
ssc install outtex
ssc install egenmore

//Load dataset study2
use "$data_dir\study2.dta", clear

//Define dataset as panel data
gen timevar = round*100 + t
xtset user_id timevar

//standardized variables for easy comparison of coefficients
egen inv_std = std(inv) if t >=15
foreach i in cpt st vs {
	egen `i'_std = std(`i'_value)
}
egen mu_std = std(mu_cumul)

//Calculate return, standard deviation, and skewness of lotteries as well as average realized return
gen exp_return = p_up * mu_up + p_down * mu_down
gen exp_sd = sqrt((p_up*mu_up*mu_up + p_down*mu_down*mu_down)-(exp_return*exp_return))
gen exp_skew = ((p_up*mu_up*mu_up*mu_up + p_down*mu_down*mu_down*mu_down) - 3*exp_return*exp_sd*exp_sd - exp_return*exp_return*exp_return)/(exp_sd*exp_sd*exp_sd)
gen mu_avg_std = mu_std / t
ereplace mu_avg_std = std(mu_avg_std)

//Label variables
label variable inv_std "Investments"
label variable vs_std "Visual salience"
label variable cpt_std "CPT"
label variable st_std "Salience"
label variable mu_avg_std "Average realized return"

// Calculate CPT value function with equal (1/n) decision weights
local alpha = 0.88
local lambda = 2.25

gen return = mu_up if up == 1
replace return = mu_down if up == 0
gen return_value = return^`alpha' if return >= 0
replace return_value = -`lambda'*(-return)^`alpha' if return < 0
gen cpt_value_n = .
forvalues i = 15/30 {
	local w = 1/`i'
	gen return_value_`i' = return_value * `w' if t <= `i'
	egen cpt_value_`i' = total(return_value_`i'), by(user_id round)
	replace cpt_value_n = cpt_value_`i' if t == `i'
	drop return_value_`i' cpt_value_`i'
}

//standardize for easy comparison
ereplace cpt_value_n = std(cpt_value_n)
//assign label
label variable cpt_value_n "1/n Decision Weights"

//Table 4
xtreg inv_std vs_std mu_avg_std, robust cluster(user_id) fe
est store A1
xtreg inv_std vs_std cpt_value_n mu_avg_std, robust cluster(user_id) fe
est store A2
xtreg inv_std vs_std exp* mu_avg_std cpt_value_n, robust cluster(user_id) fe
est store A3
xtreg inv_std vs_std cpt_std st_std exp* mu_avg_std cpt_value_n, robust cluster(user_id) fe
est store A4
recast double inv_std 
qui sum inv_std 
local ll = round(r(min),0.01)
local ul = round(r(max),0.01)
replace inv_std = round(inv_std,0.01) if inv_std <= `ll'+0.01 
replace inv_std = round(inv_std,0.01) if inv_std >= `ul'-0.01 
xttobit inv_std vs_std cpt_std st_std exp* mu_avg_std cpt_value_n, ll(`ll') ul(`ul')
est store A5
esttab A1 A2 A3 A4 A5 using "$table_dir\Table4.tex", se label starlevels(* 0.1 ** 0.05 *** 0.01) replace r2 sfmt(%9.4f)
